<?php
namespace app\admin\controller;

use app\admin\controller\Common;

class Conf extends Common
{
    public function conflist()
    {
        if (request()->isPost()){
            //dump($_POST);die;
            $data=input('post.');
            $enameArr=db('conf')->column('ename');
            //附件处理
            $imgColumn=db('conf')->where('dt_type',6)->column('ename');
            $confArr=array();
            foreach ($data as $k=>$v){
                $confArr[]=$k;
                if (is_array($v)){
                    $v=implode(',',$v);
                }
                db('conf')->where('ename',$k)->update(['value'=>$v]);
            }
            foreach ($enameArr as $k=>$v){
                if (!in_array($v,$confArr) && !in_array($v,$imgColumn)){
                    db('conf')->where('ename',$v)->update(['value'=>'']);
                }
            }

            foreach ($imgColumn as $k=>$v){
                $file=request()->file($v);
                if($_FILES[$v]['tmp_name']!=''){
                    $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
                    $imgSrc=$info->getSaveName();
                    if($imgSrc!=''){
                        db('conf')->where('ename',$v)->update(['value'=>$imgSrc]);
                    }

                }
            }
            $this->success('修改配置成功! ',url('conflist'));
            return;
        }
        $confRes=db('conf')->select();

        $this->assign('confRes',$confRes);
        return view();
    }
    public function lst()
    {
        $confRes=db('conf')->field('id,cname,ename,value,values')->paginate(6);
        $this->assign('confRes',$confRes);
        return view();
    }
    public function add(){
        if (request()->isPost()){
            $data=input('post.');
            $validate=validate('conf');
            if(!$validate->scene('add')->check($data)){
                $this->error($validate->getError());
            }
            $add=db('conf')->insert($data);
            if($add){
                $this->success('添加配置项成功! ',url('lst'));
            }else{
                $this->error('添加配置项失败! ');
            }
        }
        return view();
    }
    public function edit($id)
    {
        if (request()->isPost()){
            $data=input('post.');
            $validate=validate('conf');
            if(!$validate->scene('edit')->check($data)){
                $this->error($validate->getError());
            }
            $save=db('conf')->update($data);
            if($save){
                $this->success('修改配置项成功! ',url('lst'));
            }else{
                $this->error('修改配置项失败! ');
            }
            
        }
        $confs=db('conf')->find($id);
        $this->assign('confs',$confs);
        return view();
    }
    public function del($id){
        $del=db('conf')->delete($id);
        if($del){
            $this->success('删除配置项成功! ',url('lst'));
        }else{
            $this->error('删除配置项失败! ');
        }
    }
}
